﻿using System;
using System.Collections.Generic;
using UBlockly.Scriptable;
using UnityEngine;
using UBlockly.Compotent;
using UBlockly.Sys;

namespace UBlockly.UI
{
    public sealed class NormalNode : Node
    {
        internal override bool IsDefaultParamNode => false;
        public NormalNode(BlockObject block, NodeScriptable x) : base(block, x) { }
    }
    public sealed class CustomBranchNode : BranchNode
    {
        private int _branchCount;
        private List<LinkedList<Node>> _branchList;
        public override int BranchCount => _branchCount;

        internal override bool IsDefaultParamNode => false;

        public override bool IsNullBranch
        {
            get
            {
                foreach (var br in _branchList)
                {
                    if (br.Count > 1)
                        return false;
                }

                return true;
            }
        }
        public CustomBranchNode(int branchCount,BlockObject block,NodeScriptable x) : base(block,x)
        {
            if (branchCount <= 0)
                throw new SystemException("CustomBranchNode branchCount cannot be "+branchCount);
            _branchCount = branchCount;
            _branchList = new List<LinkedList<Node>>(_branchCount);
            for (int i = 0; i < branchCount; i++)
            {
                _branchList.Add(new LinkedList<Node>());
            }
        }
        public override void Init()
        {
            base.Init();
            for (int i = 0; i < _branchCount; i++)
            {
                var branchBlock = Block.Find($"Condition-{i}");
                if(branchBlock == null)
                    throw new SystemException($"CustomBranchNode cannot found a node:Condition-{i}");
                CreateBranchRoot(i, new EmptyScriptNode(branchBlock.GetComponent<BlockObject>()));
            }
        }
        public override LinkedList<Node> GetBranch(int id)
        {
            if(id <0 || id >=_branchCount)
                throw new SystemException("CustomBranchNode error");
            return _branchList[id];
        }

        public override Node GetBranchRoot(int id)
        {
            return GetBranch(id).First.Value;
        }
    }

    public sealed class ComplexNode : Node
    {
        internal override bool IsDefaultParamNode => false;

        public ComplexNode(BlockObject block, NodeScriptable x) : base(block, x)
        {}

        public void DisplayDetails()
        {
            if(Scriptable.TreeStrArray == null || Scriptable.TreeStrArray.Length == 0)
                throw new System.Exception("ComplexNode Unkonw error");
            BlockObject target = null;
            for (int i = 0; i < Scriptable.TreeStrArray.Length; i++)
            {
                var block = SerializeTreeNode.FromStr(Scriptable.TreeStrArray[i]).CreateDisplayBlock();
                if (target != null)
                {
                    BlockSystem.Slot(block,target);
                }

                target = block;
            }
        }
    }
}
